package com.yxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxh.base.PageEntity;
import com.yxh.dao.DeptMapper;
import com.yxh.dao.UserMapper;
import com.yxh.exception.DeptException;
import com.yxh.exception.UnitException;
import com.yxh.pojo.Dept;
import com.yxh.pojo.FineType;
import com.yxh.pojo.User;
import com.yxh.service.DeptService;
import com.yxh.util.SnowflakeIdWorker;
import com.yxh.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.List;

import static com.yxh.util.APIErrorCode.*;

/**
 * Created by asus on 2018/5/31.
 */

@Service
@Transactional
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public int save(Dept record) {
        try {
            User user=new User();
            user.setDept(record.getFatherId());
          /*  List<User> u=userMapper.selectByUser(user);
            if(u.size()!=0) return -1;*/
            return deptMapper.insert(record);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo getInformation(Dept deptJson, Integer limit, Integer offset) {
        PageHelper.startPage(limit, offset);
        List<Dept> deptList = deptMapper.selectByDept(deptJson);
        PageInfo pageInfo = new PageInfo(deptList);
        return pageInfo;
    }

    @Override
    public int update(Dept record) {
        try {
            return deptMapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int delete(String id) {
        Dept dept = new Dept();
        dept.setFatherId(id);
        List<Dept> deptList = deptMapper.selectByDept(dept);
        if(deptList.size()>0){
            throw new DeptException(DEPT_CHILDREN_NOT_DELETE_ERROR);
        }
        User user = new User();
        user.setDept(id);
        List<User> userList = userMapper.selectByUser(user);
        if (userList.size() > 0) {
            throw new DeptException(DEPT_NOT_DELETE_ERROR);
        }
        if (!StringUtil.isEmpty(id)) {
            String[] arr = id.split(",");
            int count = deptMapper.deleteByIds(arr);
            if (count == 0) {
                throw new DeptException(PARAMS_INVALID);
            }
        }
        return 0;
    }

    @Override
    public void batchDelete(List<Dept> records) {

    }

    @Override
    public Dept get(String id) {
        return null;
    }

    @Override
    public Dept get(Dept record) {
        return null;
    }

    @Override
    public List<Dept> getList(Dept record) {
        return null;
    }

    @Override
    public List<Dept> getAllList(Dept record) {
        return null;
    }

    @Override
    public List<Dept> getListPage(PageEntity<Dept> record) {
        return null;
    }


}
